package statistics.demo.statistics_demo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by 雷瞳 on 2019/11/30.
 */
public class TableAdapter extends BaseAdapter {

    private List<DataModal> mList;
    private LayoutInflater mInflater;

    public TableAdapter(Context context, List<DataModal> list) {
        this.mList = list;
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (this.mList != null) {
            return this.mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return this.mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DataModal dataModal = (DataModal) this.getItem(position);

        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = this.mInflater.inflate(R.layout.list_item, null);
            viewHolder.item1 = convertView.findViewById(R.id.item1);
            viewHolder.item2 = convertView.findViewById(R.id.item2);
            viewHolder.item3 = convertView.findViewById(R.id.item3);
            viewHolder.item4 = convertView.findViewById(R.id.item4);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.item1.setText(dataModal.getItem1());
        viewHolder.item2.setText(dataModal.getItem2());
        viewHolder.item3.setText(dataModal.getItem3());
        viewHolder.item4.setText(dataModal.getItem4());
        return convertView;
    }

    public static class ViewHolder {
        public TextView item1;
        public TextView item2;
        public TextView item3;
        public TextView item4;
    }
}
